package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemTemplateDao;
import ysq.xyg.host.model.po.HostItemTemplate;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostItemTemplateVo;
import ysq.xyg.host.service.HostItemTemplateService;

import java.util.Date;
import java.util.List;

/**
 * 房产模块-更多模版service实现
 *
 * @author congcong tao
 * @date 2020-3-6 10:12:30
 */
@Service("hostItemTemplateService")
public class HostItemTemplateServiceImpl extends BaseServiceImpl<HostItemTemplate> implements HostItemTemplateService {

    @Autowired
    private HostItemTemplateDao hostItemTemplateDao;

    @Autowired
    public void setBaseDao(HostItemTemplateDao hostItemTemplateDao) {
        this.baseDao = hostItemTemplateDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemTemplateVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:30
     */
    @Override
    public ReturnEntity page(HostItemTemplateVo hostItemTemplateVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" order_num asc, create_time desc");
        List<HostItemTemplate> list = acceptList(hostItemTemplateVo);
        PageInfo<HostItemTemplate> page = new PageInfo<HostItemTemplate>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemTemplateVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:30
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemTemplateVo hostItemTemplateVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItemTemplate hostItemTemplate;
        if (StrUtil.isEmpty(hostItemTemplateVo.getId())) {
            hostItemTemplate = new HostItemTemplate(currentUser);
            isUpate = false;
        } else {
            hostItemTemplate = selectByPrimaryKey(hostItemTemplateVo.getId());
            if (ObjectUtil.isNull(hostItemTemplate)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItemTemplate.setUpdateTime(new Date());
            hostItemTemplate.setUpdateUser(currentUser.getId());
        }
        hostItemTemplate.setAccountId(hostItemTemplateVo.getAccountId());
        hostItemTemplate.setName(hostItemTemplateVo.getName());
        hostItemTemplate.setValue(hostItemTemplateVo.getValue());
        hostItemTemplate.setUnit(hostItemTemplateVo.getUnit());
        hostItemTemplate.setRemark(hostItemTemplateVo.getRemark());
        hostItemTemplate.setOrderNum(hostItemTemplateVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostItemTemplate);
        } else {
            num = insert(hostItemTemplate);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:30
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItemTemplate hostItemTemplate = selectByPrimaryKey(id);
        if (hostItemTemplate != null) {
            hostItemTemplate.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItemTemplate.setUpdateTime(new Date());
            hostItemTemplate.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostItemTemplate);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemTemplateVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2020-3-6 10:12:30
     */
    @Override
    public List<HostItemTemplate> acceptList(HostItemTemplateVo hostItemTemplateVo){
        Example example = new Example(HostItemTemplate.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemTemplateVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemTemplateVo.getRemoveTag());
        }
        example.setOrderByClause(" order_num asc, create_time desc");
        return hostItemTemplateDao.selectByExample(example);
    }
}
